package com.itestor.iptv.modules.ne.actions;



import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.itestor.iptv.global.utils.LoggerUtils;
import com.itestor.iptv.modules.common.domain.RegionManage;
import com.itestor.iptv.modules.common.service.RegionManageService;
import com.itestor.iptv.modules.ne.service.RobotManageService;
import com.itestor.iptv.modules.ne.utils.NeConstant;
import com.itestor.iptv.modules.ne.vo.RobotCFGOBJ;
import com.itestor.iptv.modules.ne.vo.RobotFileInfoOBJ;
import com.itestor.iptv.modules.ne.vo.RobotManageOBJ;
import com.itestor.iptv.modules.ne.vo.RobotNetInfoOBJ;

/**
 * 
 * @author Mr.li
 * 
 */
public class RobotManageNaviAction extends DispatchAction {

    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger.getLogger(RobotManageNaviAction.class);
    
    private RobotManageService robotManageService;
    
    private RegionManageService regionManageService;

    
    /**
     * 新增设备
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward add(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        List<RegionManage> regionList= regionManageService.getRootRegionList();
        request.setAttribute("RA_ROOT_REGIONS", regionList);
                
        return mapping.findForward(NeConstant.FORWARD_ROBOT_ADD);
    }
    
    public ActionForward addCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward(NeConstant.FORWARD_CFG_ADD);
    }
    
    public ActionForward addFileInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward(NeConstant.FORWARD_FILE_ADD);
    }
    
    public ActionForward addNetInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        return mapping.findForward(NeConstant.FORWARD_NET_ADD);
    }
    /**
     * 修改区局
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modify(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        RobotManageOBJ bean = robotManageService.get(Integer.parseInt(id));
        
        if (bean == null) {
            return null;
        }
        
        LoggerUtils.logActionInfo("To modify the RobotManage  id " + id + ".",
                logger, request);
        
        List<RegionManage> regionList= regionManageService.getRootRegionList();
        request.setAttribute("RA_ROOT_REGIONS", regionList);
        
        request.setAttribute(NeConstant.REQUEST_ATTR_ROBOT, bean);        
        return mapping.findForward(NeConstant.FORWARD_ROBOT_MODIFY);
    }
    
    /**
     * 修改设备基本信息.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modifyCfg(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        RobotCFGOBJ bean = robotManageService.getRobotCFGOBJ(Integer.parseInt(id));
        
        if (bean == null) {
            bean =new RobotCFGOBJ();
            bean.setIdRobot(Integer.parseInt(id));
        }
        
        LoggerUtils.logActionInfo("To modify the RegionManage  idRegion " + id + ".",
                logger, request);
        
        request.setAttribute(NeConstant.REQUEST_ATTR_CFG, bean);
        
        if("true".equals(request.getParameter("flag")))
            return mapping.findForward(NeConstant.FORWARD_FILE_MODIFY);
        
        
        return mapping.findForward(NeConstant.FORWARD_CFG_SUCCESS);
    }
    
    /**
     * 配置文件参数.
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modifyFile(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        RobotFileInfoOBJ bean = robotManageService.getRobotFileInfoOBJ(Integer.parseInt(id));
        
        if (bean == null) {
            bean =new RobotFileInfoOBJ();
            bean.setIdRobot(Integer.parseInt(id));
        }
        
        LoggerUtils.logActionInfo("To modify the RegionManage  idRegion " + id + ".",
                logger, request);
        
        request.setAttribute(NeConstant.REQUEST_ATTR_FILE, bean);
        
        if("true".equals(request.getParameter("flag"))){
            return mapping.findForward(NeConstant.FORWARD_NET_MODIFY);                
        }
        
        return mapping.findForward(NeConstant.FORWARD_FILE_SUCCESS);
    }
    
    /**
     * 配置网络参数
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward modifyNetInfo(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        String id = request.getParameter("id");
        if (StringUtils.isEmpty(id)) {
            return null;
        }
        RobotNetInfoOBJ bean = robotManageService.getRobotNetInfoOBJ(Integer.parseInt(id));
        
        if (bean == null) {
            bean =new RobotNetInfoOBJ();
            bean.setIdRobot(Integer.parseInt(id));
        }
        
        LoggerUtils.logActionInfo("To modify the RegionManage  idRegion " + id + ".",
                logger, request);
        

        request.setAttribute(NeConstant.REQUEST_ATTR_NET, bean);
        
        if("true".equals(request.getParameter("flag"))){
            return mapping.findForward(NeConstant.FORWARD_DETAIL_SUCCESS);                
        }
        
        return mapping.findForward(NeConstant.FORWARD_NET_SUCCESS);
    }
    
    

    /**
     * 删除设备信息
     * @param mapping
     * @param form
     * @param request
     * @param response
     * @return ActionForward
     * @throws Exception
     */
    public ActionForward delete(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        
        String[] pvids = request.getParameterValues("id");
        if (null == pvids) {
            String pvid = request.getParameter("id");
            if (!StringUtils.isEmpty(pvid)) {
                pvids = new String[1];
                pvids[0] = pvid;
            }
        }
        if (null != pvids && pvids.length > 0) {
            
            int[] ids = new int[pvids.length];
            for (int i = 0; i < pvids.length; i++) {
                try {
                    ids[i] = Integer.parseInt(pvids[i]);
                } catch (NumberFormatException e) {
                    ids[i] = 0;
                }
            }
            LoggerUtils.logActionInfo("Delete the user - id: "
                    + StringUtils.join(pvids, ",") + ".", logger, request);
            
            robotManageService.deleteRobotCFG(ids);
            robotManageService.deleteRobotFileInfo(ids);
            robotManageService.deleteRobotNetInfo(ids);
            robotManageService.delete(ids);
            
        }

        return mapping.findForward(NeConstant.FORWARD_ROBOT_DELETE);
    }

    
    /**
     * 
     * @param pRobotManageService
     */
    public void setRobotManageService(RobotManageService pRobotManageService) {
        robotManageService = pRobotManageService;
    }
    
    /**
     * 
     * @param pRegionManageService
     */
    public void setRegionManageService(RegionManageService pRegionManageService) {
        regionManageService = pRegionManageService;
    }

    
    
    
}
